home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Setup / BCB / data.z / collate.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-09  |  4.8 KB  |  151 lines

  1. #ifndef __COLLATE_CC
  2. #define __COLLATE_CC
  3. #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
  4.  
  5. /***************************************************************************
  6.  *
  7.  * collate.cc - Definitions for the Standard Library character
  8.  * collation facet
  9.  *
  10.  *
  11.  ***************************************************************************
  12.  *
  13.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  14.  * ALL RIGHTS RESERVED *
  15.  * The software and information contained herein are proprietary to, and
  16.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  17.  * intends to preserve as trade secrets such software and information.
  18.  * This software is furnished pursuant to a written license agreement and
  19.  * may be used, copied, transmitted, and stored only in accordance with
  20.  * the terms of such license and with the inclusion of the above copyright
  21.  * notice.  This software and information or any other copies thereof may
  22.  * not be provided or otherwise made available to any other person.
  23.  *
  24.  * Notwithstanding any other lease or license that may pertain to, or
  25.  * accompany the delivery of, this computer software and information, the
  26.  * rights of the Government regarding its use, reproduction and disclosure
  27.  * are as set forth in Section 52.227-19 of the FARS Computer
  28.  * Software-Restricted Rights clause.
  29.  * 
  30.  * Use, duplication, or disclosure by the Government is subject to
  31.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  32.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  33.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  34.  * P.O. Box 2328, Corvallis, Oregon 97339.
  35.  *
  36.  * This computer software and information is distributed with "restricted
  37.  * rights."  Use, duplication or disclosure is subject to restrictions as
  38.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  39.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  40.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  41.  * then the "Alternate III" clause applies.
  42.  *
  43.  **************************************************************************/
  44.  
  45. #ifndef _RWSTD_NO_NAMESPACE
  46. namespace std {
  47. #endif
  48.  
  49.  
  50. // --------------------------------------
  51. // Facet collate<charT> member templates.
  52. // --------------------------------------
  53.  
  54. template <class charT>
  55. locale::id collate<charT>::id;
  56.  
  57. template <class charT>
  58. collate<charT>::~collate() { }
  59.  
  60. template <class charT>
  61. int collate<charT>::do_compare
  62.     (const charT* low1, const charT* high1,
  63.      const charT* low2, const charT* high2) const
  64. {
  65.   size_t len1=high1-low1;
  66.   size_t len2=high2-low2;
  67.   size_t len = len1 < len2 ? len1 : len2;
  68.   int comp;
  69.  
  70.   for (const charT *p=low1,*q=low2; len--; p++,q++)
  71.     if ((comp=coll_order(*p)-coll_order(*q))!=0)
  72.       return comp<0? -1 : 1;
  73.  
  74.   if (len1<len2)
  75.     return -1;
  76.   if (len2<len1)
  77.     return 1;
  78.   return 0;
  79. }
  80.  
  81. template <class charT>
  82. _TYPENAME collate<charT>::string_type
  83. collate<charT>::do_transform
  84.     (const charT* low, const charT* high) const
  85. {
  86.   string_type result(high-low,'\0');
  87.   _TYPENAME string_type::iterator out=result.begin();
  88.   for (const charT *p=low; p!=high; p++)
  89.     *out++=coll_order(*p);
  90.   return result;
  91. }
  92.  
  93. template <class charT>
  94. long collate<charT>::do_hash (const charT *start, const charT *end) const
  95. {
  96.   // Peter Weinberger's generic hashing algorithm, adapted by Andrew Binstock
  97.   // from a version by Allen Holub (see Andrew Binstock, "Hashing Revisited",
  98.   // Dr. Dobb's Journal, April 1996) and templatized by Rogue Wave.
  99.  
  100.   const int long_bits=CHAR_BIT*sizeof(long);
  101.   const int one_eighth=long_bits/8;
  102.   const int three_fourths=long_bits*3/4;
  103.   const int high_bits=(int)((~0L) << (long_bits-one_eighth));
  104.  
  105.   long result=0;
  106.   for (const charT *p=start; start<end; start++) {
  107.     result=(result << one_eighth) + *p;
  108.     long temp=result & high_bits;
  109.     if (temp)
  110.       result=(result^(temp>>three_fourths)) &~ high_bits;
  111.   }
  112.  
  113.   return result;
  114. }
  115.  
  116. // --------------------------------------------------------------------
  117. // Character collation by-name member templates: collate_byname<charT>
  118. // --------------------------------------------------------------------
  119.  
  120. template <class charT>
  121. collate_byname<charT>::collate_byname (const char *name, size_t refs):
  122.     collate<charT>(refs)
  123. { }
  124.  
  125. template <class charT>
  126. collate_byname<charT>::~collate_byname()
  127. { }
  128.  
  129. template <class charT>
  130. int collate_byname<charT>::do_compare
  131.     (const charT* low1, const charT* high1,
  132.      const charT* low2, const charT* high2) const
  133. {
  134.   return 0;
  135. }
  136.  
  137. template <class charT>
  138. _TYPENAME collate_byname<charT>::string_type collate_byname<charT>::do_transform
  139.     (const charT* low, const charT* high) const
  140. {
  141.   return string_type();
  142. }
  143.  
  144.  
  145. #ifndef _RWSTD_NO_NAMESPACE
  146. }
  147. #endif
  148.  
  149. #pragma option pop
  150. #endif /* __COLLATE_CC */
  151.